package com.uppfind.framework.utils.file;

import freemarker.cache.FileTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.Template;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.util.Map;


/**
 * Created by HHB on 2017/6/13.
 * 内容：
 * 传入数据:
 * 传出数据:
 */
public class WordUtil {

    public String createDoc(Map<String, Object> dataMap, HttpServletRequest request, String Name) {
        String url = "/Files/" + "派工单" + Name + ".docx";
        File outFile = new File(request.getSession().getServletContext().getRealPath("/") + "Files/" + "派工单" + Name + ".docx");

        try {
            //模板的路径
            File fir = new File(request.getSession().getServletContext().getRealPath("/") + "Files/");
            //生成文件的路径及文件名。
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "UTF-8"));

            // 使用FileTemplateLoader
            //指定模板路径
            TemplateLoader templateLoader = null;
            templateLoader = new FileTemplateLoader(fir);
            String tempname = "workOrder.xml";

            Configuration cfg = new Configuration();
            cfg.setTemplateLoader(templateLoader);
            Template t = cfg.getTemplate(tempname, "UTF-8");

            t.process(dataMap, out);
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        String ur0 = outFile.getAbsolutePath();
        try {
            String url1 = outFile.getCanonicalPath();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String url2 = outFile.getPath();
        String url3 = outFile.getParent();


        return url;
    }
}
